﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RCMS.Web.WebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:GridViewPager runat=server></{0}:GridViewPager>")]
    public class GridViewPager : WebControl, IPostBackEventHandler
    {
        public event EventHandler<GridViewPageEventArgs> PageIndexChanging;

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Table;
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (PageCount == 0)
            {
                return;
            }

            string s = "<tr>";
            if (PageIndex > 0)
            {
                s += string.Format("<td><a href=\"{0}\">{1}</a></td>", Page.ClientScript.GetPostBackClientHyperlink(this, "0"), "首页");
                //s += string.Format("<td><a href=\"{0}\">{1}</a></td>", Page.ClientScript.GetPostBackClientHyperlink(this, (PagerIndex - 1).ToString()), "上一页");
            }
            int minindex = (PageIndex / 10) * 10;
            int maxindex = minindex + 10 < PageCount ? minindex + 10 : PageCount;
            if (minindex > 0)
            {
                s += string.Format("<td><a href=\"{0}\">{1}</a></td>", Page.ClientScript.GetPostBackClientHyperlink(this, (minindex-1).ToString()), "<<");
            }

            for (int i = minindex; i < maxindex; i++)
            {
                if (i == PageIndex)
                {
                    s += string.Format("<td><span>{0}</span></td>", i + 1);
                }
                else
                {
                    s += string.Format("<td><a href=\"{0}\">{1}</a></td>", Page.ClientScript.GetPostBackClientHyperlink(this, i.ToString()), i + 1);
                }
            }
            if (maxindex < PageCount)
            {
                s += string.Format("<td><a href=\"{0}\">{1}</a></td>", Page.ClientScript.GetPostBackClientHyperlink(this, maxindex.ToString()), ">>");
            }
            if (PageIndex < PageCount - 1)
            {
                //s += string.Format("<td><a href=\"{0}\">{1}</a></td>", Page.ClientScript.GetPostBackClientHyperlink(this, (PagerIndex + 1).ToString()), "下一页");
                s += string.Format("<td><a href=\"{0}\">{1}</a></td>", Page.ClientScript.GetPostBackClientHyperlink(this, (PageCount - 1).ToString()), "末页");
            }
            s += "</tr>";

            writer.Write(s);
        }



        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("0")]
        [Localizable(true)]
        public int PageCount
        {
            get
            {
                if (ViewState["GridViewPager_PageCount"] == null)
                {
                    ViewState["GridViewPager_PageCount"] = 0;
                }

                int s = (int)ViewState["GridViewPager_PageCount"];
                return s;
            }

            set
            {
                ViewState["GridViewPager_PageCount"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(0)]
        [Localizable(true)]
        public int PageIndex
        {
            get
            {
                if (ViewState["GridViewPager_PageIndex"] == null)
                {
                    ViewState["GridViewPager_PageIndex"] = 0;
                }
                int s = (int)ViewState["GridViewPager_PageIndex"];
                return s;
            }

            set
            {
                ViewState["GridViewPager_PageIndex"] = value;
            }
        }

        #region IPostBackEventHandler 成员

        public void RaisePostBackEvent(string eventArgument)
        {
            PageIndex = int.Parse(eventArgument);
            GridViewPageEventArgs e = new GridViewPageEventArgs(PageIndex);
            this.PageIndexChanging(this, e);
        }

        #endregion

        #region IPostBackEventHandler 成员

        void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
        {
            this.RaisePostBackEvent(eventArgument);
        }

        #endregion
    }
}
